class Twitter {

    //关注列表
    private Map<Integer, Set> followerMap;
    // 推文列表
    private Map<Integer, Tweet> tweetMap;

    public Twitter() {
        followerMap = new HashMap();
        tweetMap = new HashMap();
    }
    
    public void postTweet(int userId, int tweetId) {
        Tweet tweet = tweetMap.getOrDefault(userId, new Tweet());
        Tweet next = tweet.next;
        tweet.next = new Tweet(tweetId, ++index);
        tweet.next.next = next;
        tweetMap.put(userId,tweet);    
    }
    
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> ans = new ArrayList();
        Set<Integer> follows = followerMap.getOrDefault(userId, new HashSet());
        follows.add(userId);
        //优先队列放推文节点
        Queue<Tweet> queue = new PriorityQueue<>((t1,t2)->t2.index-t1.index);
        for(Integer followeeId : follows){
            Tweet tweet = tweetMap.getOrDefault(followeeId, new Tweet());
            if(tweet.next==null)continue;
            queue.offer(tweet.next);
        }
        while(queue.peek()!=null&&ans.size()<10){
            Tweet tweet = queue.poll();
            ans.add(tweet.id);
            if(tweet.next!=null)
            queue.offer(tweet.next);
        }
        return ans;
    }
    
    public void follow(int followerId, int followeeId) {
        Set<Integer> set = followerMap.getOrDefault(followerId, new HashSet<Integer>());
        set.add(followeeId);
        followerMap.put(followerId,set);
    }
    
    public void unfollow(int followerId, int followeeId) {
        if(followerMap.get(followerId)==null)return;
        if(!followerMap.get(followerId).contains(followeeId))return;
        followerMap.get(followerId).remove(followeeId);
    }

    int index=0;

    class Tweet{
        private int id;
        private Tweet next;
        private int index;
        public Tweet(){
            this.id = 0;
            this.index = 0;
        };
        public Tweet(int id, int index){
            this.id = id;
            this.index = index;
        };
    }
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */